<template>
  <div>
    <tableComponents :pagination="{ currentPage, pageSize, total: list.all_num }" @handleCurrentChange="handleCurrentChange" @handleSizeChange="handleSizeChange" @sendUserSearch="receive_search" :isShow="false" :isHaveExport="false" :isHavePagination="false" @toAdd="openAdd" :isHaveAdd="true">
      <template v-slot:table>
        <el-table ref="multipleTable" :data="list" stripe v-loading="tableLoading" header-row-class-name="head_color" tooltip-effect="dark" style="width: 99.8%" @selection-change="handleSelectionChange">

          <el-table-column type="selection" class="tc" width="35"></el-table-column>
          <!--   <el-table-column prop="sort" sortable :sort-method="sorta" label="排序" min-width="80">
            <template slot-scope="scope">
              <el-input @change="sortNew(scope.$index, scope.row)" v-model.number="scope.row.sort" autocomplete="off" size="mini"></el-input>
            </template>
          </el-table-column> -->

          <el-table-column prop="picture" header-align="center" align="center" min-width="80" label="图片">
            <template slot-scope="scope">
              <el-popover placement="right" title trigger="hover">
                <img :src="scope.row.piclink" style="width: 500px" />
                <img slot="reference" :src="scope.row.piclink" :alt="scope.row.piclink" style="width: 50px" />
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="景区信息" min-width="120">
            <template slot-scope="scope">
              <div class="tab_div">
                <div>
                  <span>景区名称：</span>
                  <span>{{ scope.row.title }}</span>
                </div>
                <div>
                  <span>金额：</span>
                  <span>{{ Number(scope.row.money  || 0)}}</span>
                </div>
                <div>
                  <span>推荐激励值：</span>
                  <span>{{ Number(scope.row.tjjlz  || 0)}}</span>
                </div>
                <div>
                  <span>抢红包激励值：</span>
                  <span>{{ Number(scope.row.hbjlz  || 0)}}</span>
                </div>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="景区内容" min-width="180">
            <template slot-scope="scope">
              <div class="tab_div">
                <div>
                  <span>{{ scope.row.content }}</span>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="picture" min-width="220" label="图片">
            <template slot-scope="scope">
              <div>
                <div v-if="parsePiclink(scope.row.piclink1) && parsePiclink(scope.row.piclink1).length > 0">
                  <el-image style="width: 50px; height: 50px;margin:5px" fit="cover" :src="item" :preview-src-list="parsePiclink(scope.row.piclink1)" v-for="(item,i) in parsePiclink(scope.row.piclink1)" :key="i">
                  </el-image>
                </div>
                <div v-else>暂无图片</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="激活" min-width="120">
            <template slot-scope="scope">
              <el-switch class="zdySwith" :width="82" v-model="scope.row.show" active-color="#1aad19" inactive-color="#ffffff" active-text="on" inactive-text="off" active-value="1" @change="handleSwitch($event, scope.row)" inactive-value="0"></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="时间信息" min-width="220">
            <template slot-scope="scope">
              <div class="tab_div">
                <div>
                  <span>添加时间：</span>
                  <span>{{ scope.row.created_time | getTimeFormat }}</span>
                </div>
                <div>
                  <span>修改时间：</span>
                  <span>{{ scope.row.update_time | getTimeFormat }}</span>
                </div>
              </div>
            </template>
          </el-table-column>

          <!-- <el-table-column label="状态" min-width="65">
            <template slot-scope="scope">
              <el-tag type="warning" v-if="scope.row.is_buy == 0" effect="dark">未卖出</el-tag>
              <el-tag type="danger" v-else-if="scope.row.is_buy == 1" effect="dark">已下单未支付</el-tag>
              <el-tag type="success" v-else-if="scope.row.is_buy == 2" effect="dark">已下单已支付</el-tag>
            </template>
          </el-table-column> -->
          <el-table-column label="操作" min-width="120">
            <template slot-scope="scope">
              <el-row class="myblank">
                <el-button :loading="$store.state.isLoading" size="mini" @click="openEdit(scope.row)" type="primary">编辑</el-button>
                <el-button :loading="$store.state.isLoading" size="mini" type="danger" @click="openDelete(scope.row.id)">删除</el-button>
              </el-row>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template v-slot:footerLeft>
        <a class="aBtn aBtn_1 aBtn_lan2" @click="moreDelete">删除</a>
      </template>
    </tableComponents>

    <el-dialog :close-on-click-modal="false" :title="isEdit ? '编辑' : '添加'" class="tl size_13" width="800px" :visible.sync="dialogFormVisible">
      <el-form :model="edit_data" ref="form" label-width="120px">
        <el-form-item label="景区名称：">
          <el-col :span="12">
            <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="edit_data.title" autocomplete="off"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="金额：">
          <el-col :span="12">
            <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="edit_data.money" autocomplete="off"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="推荐激励值：">
          <el-col :span="12">
            <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="edit_data.tjjlz" autocomplete="off"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="抢红包激励值：">
          <el-col :span="12">
            <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="edit_data.hbjlz" autocomplete="off"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="景区图标" prop="piclink">
          <el-col :span="12">
            <el-input v-model="edit_data.piclink" disabled></el-input>
            <uploadpic :piclink="edit_data.piclink" :width="150" :height="150" :real_width="100" :real_height="100" :is_more="false" @send_pic="onSuccess"></uploadpic>
          </el-col>
        </el-form-item>
        <el-form-item label="景区图片：" prop="fileList">
          <el-col :span="24" class="mar_bottom_20">
            <el-col :span="24">
              <uploadpic :file="fileList" :is_tip="false" :width="150" :height="150" :real_width="150" :real_height="150" :is_more="true" @send_pic="onSuccess1" @del_pic="del_pic1"></uploadpic>
            </el-col>
          </el-col>
        </el-form-item>
        <el-form-item label="景区内容：">
          <el-col :span="12">
            <el-input type="textarea" :rows="6" clearable placeholder="请输入" v-model="edit_data.content" autocomplete="off"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-button :loading="$store.state.isLoading" type="primary" @click="saveEdit(edit_data)">保 存</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- <keep-alive>
      <component :is="'xzuser'" :show="shows" :isAll="false" showType="list" @sendShop="getSend" @isshow="getShow"></component>
    </keep-alive> -->
  </div>
</template>
<script>
// import xzuser from "@/components/currency/xz_user.vue";
import uploadpic from "@/components/page/pic/up_pic";
// import { dzScenicSettingSearchData } from "@/assets/js/searchData/searchData"; //引入搜索的数据
export default {
  data() {
    return {
      fileList: [],
      queryObj: {},
      //   searchDataInfo: dzMarketListSearchData,
      current: "",
      shows: false,
      isEdit: false,
      dialogFormVisible: false,
      edit_data: {
        piclink: "",
      },
      tableLoading: false,
      list: [],
      pageSize: 10,
      currentPage: 1,
      searchAll: {},
      multipleSelection: [],
    };
  },
  components: {
    uploadpic,
  },
  methods: {
    parsePiclink(piclink) {
      if (piclink) {
        return JSON.parse(piclink);
      } else {
        return [];
      }
    },
    del_pic1(val) {
      this.fileList.splice(val, 1);
    },
    onSuccess1(shop_list) {
      this.fileList = this.fileList.concat(shop_list);
    },
    getShow(val) {
      this.shows = val;
    },
    getSend(info) {
      this.edit_data.username = info.username;
      this.edit_data.uid = info.id;
    },
    xzshop() {
      this.shows = true;
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    moreDelete() {
      //批量删除
      var str = "";
      if (this.multipleSelection.length <= 0) {
        this.$message.error("未选择批量删除");
      } else {
        for (var i in this.multipleSelection) {
          str += this.multipleSelection[i].id + "@";
        }
        str = str.slice(0, -1);
        this.$swal({
          title: "您确定要删除这些信息吗",
          text: "删除后将无法恢复，请谨慎操作！",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: "是的，我要删除",
          cancelButtonText: "容我三思",
        }).then((willDelete) => {
          if (willDelete.value) {
            this.$api.getDz.del_scenic_setting({ id_str: str }).then((res) => {
              if (res.data.code == 200) {
                this.$swal("删除成功！", res.data.result, "success");
                this.getNewsAll();
              } else {
                this.$swal("删除失败！", res.data.result, "error");
              }
            });
          }
        });
      }
    },
    openDelete(id) {
      //删除
      this.$swal({
        title: "您确定要删除这些信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思",
      }).then((willDelete) => {
        if (willDelete.value) {
          this.$api.getDz.del_scenic_setting({ id_str: id }).then((res) => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", res.data.result, "success");
              this.getNewsAll();
            } else {
              this.$swal("删除失败！", res.data.result, "error");
            }
          });
        }
      });
    },

    openAdd() {
      this.edit_data = {
        piclink: "",
      };
      this.fileList = [];
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    onSuccess(pic) {
      this.edit_data.piclink = pic;
    },
    openEdit(item) {
      this.isEdit = true;
      this.edit_data = JSON.parse(JSON.stringify(item));
      this.fileList = [];
      let arr = [];
      if (this.edit_data.piclink1) {
        arr = this.parsePiclink(this.edit_data.piclink1);
        arr.forEach((item, i) => {
          this.fileList.push({ piclink: item });
        });
      }
      this.dialogFormVisible = true;
    },
    receive_search(obj) {
      this.currentPage = 1;
      this.searchAll = obj;
      this.getNewsAll();
    },
    handleSwitch(val, item) {
      var params = item;
      params.show = val;
      this.$api.getDz.edit_scenic_setting(params).then((res) => {
        if (res.data.code != 200) {
          this.$swal("编辑失败！", res.data.result, "error");
          this.getNewsAll();
        }
      });
    },

    sorta(a, b) {
      return b - a;
    },
    sortNew(index, item) {
      var params = item;
      params.sort = item.sort;
      this.$api.getDz.edit_scenic_setting(params).then((res) => {
        if (res.data.code == 200) {
          return;
        } else {
          this.$swal("编辑失败！", res.data.result, "error");
          this.getNewsAll();
        }
      });
    },
    saveEdit(item) {
      var params = {};
      params = JSON.parse(JSON.stringify(item));
      var arr = [];
      this.fileList.forEach((piclink) => {
        arr.push(piclink.piclink);
      });
      params.piclink1 = JSON.stringify(arr);
      if (this.isEdit == false) {
        this.$api.getDz.add_scenic_setting(params).then((res) => {
          if (res.data.code == 200) {
            this.dialogFormVisible = false;
            this.currentPage = 1;
            this.getNewsAll();
            this.$swal("添加成功！", res.data.result, "success");
          } else {
            this.$swal("添加失败！", res.data.result, "error");
          }
        });
      } else {
        this.$api.getDz.edit_scenic_setting(params).then((res) => {
          if (res.data.code == 200) {
            this.dialogFormVisible = false;
            this.getNewsAll();
            this.$swal("编辑成功！", res.data.result, "success");
          } else {
            this.$swal("编辑失败！", res.data.result, "error");
          }
        });
      }
    },
    getNewsAll() {
      this.tableLoading = true;
      var params = {};
      params = JSON.parse(JSON.stringify(this.searchAll));
      params.page = this.currentPage || 1;
      params.page_size = this.pageSize || "";
      for (var i in params) {
        if (!params[i]) {
          params[i] = "";
        }
      }
      var query = JSON.parse(JSON.stringify(this.queryObj || {}));
      var obj = Object.assign(query, params);
      this.$api.getDz.get_scenic_setting(obj).then((res) => {
        if (res.data.code == 200) {
          res.data.result.forEach((item, i) => {
            item.money = item.money ? Number(item.money) : "";
            item.tjjlz = item.tjjlz ? Number(item.tjjlz) : "";
            item.hbjlz = item.hbjlz ? Number(item.hbjlz) : "";
          });
          this.list = res.data.result;
        }
        setTimeout(() => {
          this.tableLoading = false;
        }, 200);
      });
    },
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.getNewsAll();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getNewsAll();
    },
  },
  created() {
    if (this.$route.query) {
      this.queryObj = this.$route.query;
      if (this.$route.query.page) {
        this.currentPage = this.$route.query.page;
        delete this.queryObj.page;
      }
    }
    this.getNewsAll();
  },
  computed: {},
};
</script>

<style lang="less" scoped>
.myblank button {
  margin: 5px 5px 0 0;
}
</style>
